package com.fun.mysql;

import com.fun.frame.SourceCode;
import com.fun.profile.SqlConstant;

import java.sql.*;

/**
 * 数据库基础类，主要公共的获取连接和操作对象
 */
public class SqlBase extends SourceCode {

	/**
	 * 获取数据库连接
	 *
	 * @param url      地址，包括端口
	 * @param user     用户名
	 * @param passowrd 密码
	 * @return
	 */
	public static Connection getConnection(String url, String user, String passowrd) {
		Connection connection = null;
		try {
			Class.forName(SqlConstant.DRIVE);
		} catch (ClassNotFoundException e) {
			output("加载驱动程序失败！", e);
		}
		try {
			connection = DriverManager.getConnection(url, user, passowrd);
		} catch (SQLException e) {
			output("数据库连接失败！", e);
		}
		return connection;
	}

	/**
	 * 获取statement对象
	 *
	 * @param connection
	 * @return
	 */
	public static Statement getStatement(Connection connection) {
		Statement statement = null;
		try {
			statement = connection.createStatement();
		} catch (SQLException e) {
			output("获取数据库连接失败！", e);
		} catch (ExceptionInInitializerError e) {
			output("初始化失败!", e);
		}
		return statement;
	}

	/**
	 * 执行sql语句,查询语句，返回ResultSet，并不关闭连接
	 *
	 * @param connection
	 * @param statement
	 * @param sql
	 * @return
	 */
	public static ResultSet excuteQuerySql(Connection connection, Statement statement, String sql) {
		try {
			if (connection != null && !connection.isClosed()) {
				ResultSet resultSet = statement.executeQuery(sql);
				return resultSet;
			}
		} catch (SQLException e) {
			output(sql, e);
		}
		return null;
	}

	/**
	 * 执行sql语句，非query语句，不关闭连接
	 *
	 * @param connection
	 * @param statement
	 * @param sql
	 */
	public static void excuteUpdateSql(Connection connection, Statement statement, String sql) {
		try {
			if (!connection.isClosed()) statement.executeUpdate(sql);
		} catch (SQLException e) {
			output(sql, e);
		}

	}

	/**
	 * 关闭数据库资源
	 *
	 * @param connection
	 * @param statement
	 */
	public static void mySqlOver(Connection connection, Statement statement) {
		try {
			if (connection == null || connection.isClosed()) return;
			statement.close();
			connection.close();
		} catch (SQLException e) {
			output("关闭数据库链接失败！", e);
		}
	}
}
